package logic

import (
	"2211a_mayi/service/mayi"
	"context"

	"2211a_mayi/api/internal/svc"
	"2211a_mayi/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PublishGoodsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPublishGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PublishGoodsLogic {
	return &PublishGoodsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PublishGoodsLogic) PublishGoods(req *types.PublishGoodsRequest) (resp *types.Response, err error) {
	_, err = l.svcCtx.Service.CreateGoods(l.ctx, &mayi.CreateGoodsRequest{
		GoodsTitle: req.GoodsTitle,
		GoodsCover: req.GoodsCover,
		GoodsPrice: float32(req.GoodsPrice),
		GoodsNum:   int64(req.GoodsStock),
	})

	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  "发布失败",
		}, err
	}

	return &types.Response{
		Code: 200,
		Msg:  "发布成功",
	}, nil
}
